package com.kaka.bus.engine.home.info.message;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.MessageAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.MessageBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.U;
import com.kaka.bus.util.logger.Logger;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/12/25.
 * e-mail : ${email}
 * desc :
 */

public class MessageListActivity extends BaseActivity<MessageListPresenter> implements MessageListContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.message_recy)
    RecyclerView mMessageRecy;
    @BindView(R.id.message_smart)
    SmartRefreshLayout mMessageSmart;
    @BindView(R.id.message_muti)
    MutipleLayout mMessageMuti;
    private int mPage = 1;
    private int mPageSize = 20;

    private MessageAdapter mMessageAdapter;

    private List<MessageBean> mDatas = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("消息列表");

        mMessageRecy.setLayoutManager(new LinearLayoutManager(this));
        mMessageAdapter = new MessageAdapter(R.layout.item_message, mDatas,this);
        mMessageRecy.setAdapter(mMessageAdapter);
        mMessageAdapter.bindToRecyclerView(mMessageRecy);
        mMessageMuti.setEmptyText("您当前暂无消息通知!");
        mMessageMuti.setEmptyImage(R.drawable.empty_view_kongtai);
        mMessageMuti.setStatus(MutipleLayout.Loading);
    }

    @Override
    protected void initData() {
        getData();
    }

    public void getData() {
        mPage = 1;
        mDatas.clear();
        mPresenter.initData(getUserId(), getToken(), 1, mPageSize);
    }

    @Override
    public void onError() {
        mPage--;
        mMessageSmart.finishLoadmore(0);
        mMessageMuti.setStatus(MutipleLayout.Error);
    }

    @Override
    public void onResultStart(HttpWrapper<List<MessageBean>> data) {
        mMessageSmart.finishRefresh(0);
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            if (data.data != null && data.data.size() > 0) {
                mMessageAdapter.addData(data.data);
                mMessageMuti.setStatus(MutipleLayout.Success);
            } else {
                mMessageMuti.setStatus(MutipleLayout.Empty);
            }
        } else if (Constant.ERROR_CODE.equals(data.getCode())) {
            showMessage(data.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    public void onResultLoadMore(HttpWrapper<List<MessageBean>> o) {
        mMessageSmart.finishLoadmore(0);
        if (Constant.SUCCESS_CODE.equals(o.getCode())) {
            if (Constant.SUCCESS_CODE.equals(o.getCode())) {
                if (o.data != null && o.data.size() > 0) {
                    mMessageAdapter.addData(o.data);
                } else {
                    showMessage("没有更多数据了");
                    mPage--;
                }
            }
        } else if (Constant.ERROR_CODE.equals(o.getCode())) {
            showMessage(o.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(o.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    protected void initListener() {
        mMessageSmart.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        mMessageSmart.setOnLoadmoreListener(refreshlayout -> {
            mPresenter.loadMore(getUserId(), getToken(), ++mPage, mPageSize);
        });

        mMessageAdapter.setOnItemClickListener((adapter, view, position) -> {
            MessageBean item = mDatas.get(position);
            item.setOpen(item.isOpen()?false:true);
            Logger.i(item.getStatus());
            if (item.getStatus().equals(Constant.MESSAGE_NO)) {
                mPresenter.updateMessage(getUserId(), item.getId(), getToken(),position);
                item.setStatus(Constant.MESSAGE_YES);
            } else if (item.getStatus().equals(Constant.MESSAGE_YES)) {
                mMessageAdapter.notifyItemChanged(position);
            }
        });
    }

    @Override
    public void updateMessageSuccess(HttpWrapper<String> wrapper, int position) {
        Logger.i(wrapper.code);
        if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            mMessageAdapter.notifyItemChanged(position);
        } else if(Constant.TOKEN_TIMEOUT.equals(wrapper.code)){
            U.tokenOut(this);
        }else {
            showMessage(wrapper.info);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
